package com.edu.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.edu.common.core.domain.entity.SysUser;
import com.edu.common.utils.DateUtils;
import com.edu.common.utils.SecurityUtils;
import com.edu.system.domain.ManageScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.ManageMapper;
import com.edu.system.domain.Manage;
import com.edu.system.service.IManageService;

/**
 * 管理范围Service业务层处理
 * 
 * @author zqq
 * @date 2023-01-22
 */
@Service
public class ManageServiceImpl implements IManageService
{
    @Autowired
    private ManageMapper manageMapper;

    /**
     * 查询管理范围列表
     * 
     * @param conditions 管理范围
     * @return 管理范围
     */
    @Override
    public List<Manage> selectManageList(Map<String,Object> conditions)
    {
        if (conditions.containsKey("scopeIds")){
            List<String> adminIds = new ArrayList<>();
            adminIds.add("user|"+ SecurityUtils.getUserId());
            adminIds.add("all");//全公司管理员课件标识
            for (String scopeId  : (Set<String>) conditions.get("scopeIds")){
                adminIds.add(scopeId);
            }
            conditions.put("adminIds",adminIds);
            conditions.remove("scopeIds");
        }
        return manageMapper.selectManageList(conditions);
    }

    @Override
    public List<ManageScope> selectManageByList(String companyId,String table)
    {
        return manageMapper.selectManageByList(companyId,table);
    }

    @Override
    public Manage selectManageInfo(Map<String,Object> conditions)
    {
        return manageMapper.selectManageInfo(conditions);
    }

    /**
     * 新增管理范围
     *
     * @param manage 管理范围
     * @return 结果
     */
    @Override
    public int insertManage(Manage manage, String table)
    {
        SysUser userInfo = SecurityUtils.getLoginUser();
        //定义数据
        String time = DateUtils.getTime();
        List<Manage> insertList = new ArrayList<>();
        Set<String> allDeptParent = userInfo.getAllDeptParent();
        for (String id : allDeptParent){
            Manage data = new Manage();
            data.setAdminId(id);
            data.setTargetId(manage.getTargetId());
            data.setStatus(1);
            data.setIsDelete(0);
            data.setCreateTime(time);
            data.setUpdateTime(time);
            data.setCreateUserId(userInfo.getUserId());
            data.setUpdateUserId(userInfo.getUserId());
            data.setCompanyId(userInfo.getCompanyId());
            insertList.add(data);
        }
        return manageMapper.insertManageAll(insertList,table);
    }

    @Override
    public int insertManageAll(List insertList, String table)
    {
        return manageMapper.insertManageAll(insertList,table);
    }

    @Override
    public int insertManageInfo(Manage manage)
    {
        return manageMapper.insertManageInfo(manage);
    }

    /**
     * 修改管理范围
     * 
     * @param Manage 管理范围
     * @return 结果
     */
    @Override
    public int updateManage(Map<String,Object> Manage)
    {
        return manageMapper.updateManage(Manage);
    }
}
